import {
  equipmentClassArr,
  checkType,
  equipmentStatusArr,
  checkStatusArr,
  equipmentBelongToArr
} from '@/views/quality/equipmentManagement/constant/dictionary'
import dataFilter from '../../constant/dataFilter'
import reckoningTime from '../../constant/reckoningTime'
import moment from 'moment'
import UploadList from '@/components/UploadList'
let _monthDays = 30;
//editModel:新增2编辑1查看0|form：表单数据
const ownFormList = (editModel, isHasTime, isAccessControl, form) => {
  let formList = [
    {
      label: '设备名称',
      keyName: 'mdId',
      slot: true,
      rules: editModel == 2 ? [{ required: true, message: '请选择设备名称', trigger: 'change' }] : []
    },
    {
      label: '设备编号',
      keyName: 'mdCode',
      type: 'BaseInput',
      disabled: true,
    },
    {
      label: '规格型号',
      keyName: 'mdModelId',
      slot: true,
    },
    {
      label: '设备分类',
      keyName: 'mdClassify',
      type: 'a-select',
      data: equipmentClassArr,
      disabled: editModel == 0,
      customRender(e) {
        const obj = equipmentClassArr.find((v) => v.value === e)
        return Object.keys(obj).length ? obj.label : null
      }
    },
    {
      label: '设备用途',
      keyName: 'purpose',
      type: 'BaseInput',
      disabled: editModel == 0
    },
    {
      label: '机械设备编码（物资编码）',
      keyName: 'materialCode',
      type: 'BaseInput',
      disabled: editModel == 0,
    },
    {
      label: '采购单价',
      keyName: 'purchasePrice',
      type: 'a-input-number',
      disabled: editModel == 0,
    },
    {
      label: '品牌厂家名称',
      keyName: 'manufacturerName',
      type: 'BaseInput',
      disabled: editModel == 0,
    },
    {
      label: '出厂编号',
      keyName: 'manufacturerCode',
      type: 'BaseInput',
      disabled: editModel == 0,
    },
    {
      label: '检定方式',
      keyName: 'appraisalType',
      type: 'a-select',
      data: checkType,
      disabled: editModel == 0,
      customRender(e) {
        const obj = checkType.find((v) => v.value === e)
        return Object.keys(obj).length ? obj.label : null
      }
    },
    {
      type: 'a-date-picker',
      keyName: 'beforeAppraisalDate',
      label: '上次检定时间',
      attrs: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD',
        disabledDate(current) {
          return current && current >= moment().startOf('day');
        },
      },
      disabled: editModel == 0,
      change: (dates) => {
        if (dates && /^[1-9]\d*$/.test(form.appraisalCycle)) {
          form.afterAppraisalDate = reckoningTime(-(form.appraisalCycle * _monthDays), dates)
        } else {
          form.afterAppraisalDate = null;
        }
      }
    },
    {
      label: '检定周期（月数）',
      keyName: 'appraisalCycle',
      type: 'BaseInput',
      disabled: editModel == 0,
      change: (e) => {
        let _v = e.target.value;
        if (_v && /^[1-9]\d*$/.test(_v) && form.beforeAppraisalDate) {
          form.afterAppraisalDate = reckoningTime(-(_v * _monthDays), form.beforeAppraisalDate)
        } else {
          form.afterAppraisalDate = null;
        }
      }
    },
    {
      type: 'a-date-picker',
      keyName: 'afterAppraisalDate',
      label: '下次检定日期',
      disabled: true,
      attrs: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD'
      }
    },
    {
      label: '检定状态',
      keyName: 'appraisalStatus',
      type: 'a-select',
      data: checkStatusArr,
      disabled: editModel == 0,
      customRender(e) {
        const obj = checkStatusArr.find((v) => v.value === e)
        return Object.keys(obj).length ? obj.label : null
      }
    },
    {
      label: '设备状态',
      keyName: 'mdStatus',
      type: 'a-select',
      disabled: true,
      data: dataFilter(equipmentStatusArr, ['0', '1', '2', '3', '4', '9']),
      customRender(e) {
        const obj = equipmentStatusArr.find((v) => v.value === e)
        return Object.keys(obj).length ? obj.label : null
      }
    },
    {
      label: '设备归属',
      keyName: 'mdBelongTo',
      type: 'a-select',
      disabled: true,
      data: equipmentBelongToArr,
    },
    {
      label: '创建人',
      keyName: 'creatorName',
      disabled: true,
      type: 'BaseInput',
    },
    {
      type: 'a-date-picker',
      keyName: 'createTime',
      disabled: true,
      label: '创建时间',
      attrs: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD'
      },
    },
    {
      type: 'a-date-picker',
      keyName: 'inProjectTime',
      label: '进场时间',
      disabled: editModel == 0,
      attrs: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD'
      }
    },
    {
      type: 'a-date-picker',
      keyName: 'outProjectTime',
      label: '退场时间',
      disabled: editModel == 0,
      attrs: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD'
      }
    },
    {
      label: '备注',
      keyName: 'remarks',
      type: 'BaseTextarea',
      disabled: editModel == 0,
      attrs: {
        maxLength: 300
      },
      span: 24
    },
    {
      keyName: 'ownPicFileList',
      type: UploadList,
      label: '设备图片',
      disabled: editModel == 0,
      attrs: {
        uploadType: 'image',
        businessCode: 'PM44',
        isMultiple: true,
        editModel,
        uploadArrys: form.ownPicFileList || []
      },
      span: 24,
      fileArrysData: (e) => {
        form.ownPicFileList = [...e]
      }
    },
    {
      keyName: 'ownAnnexFileList',
      type: UploadList,
      disabled: editModel == 0,
      attrs: {
        businessCode: 'PM44',
        isMultiple: true,
        uploadArrys: form.ownAnnexFileList || [],
        isShow:editModel != 0,
      },
      label: '附件上传',
      span: 24,
      fileArrysData: (e) => {
        form.ownAnnexFileList = [...e]
      }
    }
  ]
  if (!isHasTime) {
    formList.forEach((item) => {
      if (item.keyName == 'inProjectTime' || item.keyName == 'outProjectTime') {
        item.hidden = true;
      }
    })
  }
  if (isAccessControl) {
    formList.unshift({
      label: '所属单位',
      type: 'orgTree',
      keyName: 'branchCompanyId',
      disabled: editModel == 0 || editModel == 1,
      hideProject: true,
      isAutoSelectFirst: editModel == 2 ? true : false,
      checkStrictly: true,
    })
  } else {
    formList.unshift({
      label: '所属单位',
      keyName: 'branchCompanyName',
      type: 'BaseInput',
      disabled: editModel == 0 || editModel == 1,
    })
  }
  return formList;
}

export {
  ownFormList
}